using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class NewsDAL
    {
        public static int Save(NewsInfo info)
        {
            string sql = string.Format("insert into [news]([AdminID],[NewsTypeID],[Title],[Img],[Context],[ClickCount],[Sort],[CreateDate]) values(@AdminID,@NewsTypeID,@Title,@Img,@Context,@ClickCount,@Sort,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@NewsTypeID", info.NewsTypeID),
                new SqlParameter("@Title", info.Title),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@ClickCount", info.ClickCount),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(NewsInfo info)
        {
            string sql = string.Format("update [news] set [AdminID]=@AdminID,[NewsTypeID]=@NewsTypeID,[Title]=@Title,[Img]=@Img,[Context]=@Context,[Sort]=@Sort where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@NewsTypeID", info.NewsTypeID),
                new SqlParameter("@Title", info.Title),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static NewsInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[news]");
            return SimpleData(dr);
        }
        public static List<NewsInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[news]");
            return ListSimpleData(dt);
        }
        public static bool IncClickCount(int id)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [news] set ClickCount=ClickCount+1 where ID={0}", id));
        }
        public static List<NewsInfo> FindPage(int newstypeid,string keyword, int adminid, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [news].AdminID={0}", adminid);
            if (newstypeid != 0)
            {
                filter.AppendFormat(" and [news].NewsTypeID ={0}", newstypeid);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [news].Title like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [news].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[news]", out count);
            return ListSimpleData(dt);
        }
        public static List<NewsInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [news]"));
            return ListSimpleData(dt);
        }
        public static List<NewsInfo> FindTop(int adminid, string newstypeids, int top)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(newstypeids))
            {
                dt = CommonDAL.GetDataTable(string.Format("select top {0} * from [news] where AdminID={1} and NewsTypeID in ({2}) order by Sort", top, adminid, newstypeids));
            }
            return ListSimpleData(dt);
        }
        private static NewsInfo SimpleData(DataRow dr)
        {
            NewsInfo info = new NewsInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.NewsTypeID = DataConvert.ToInt(dr["NewsTypeID"]);
                info.Title = DataConvert.ToString(dr["Title"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.ClickCount = DataConvert.ToInt(dr["ClickCount"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateDateText = info.CreateDate.ToString("yyyy-MM-dd");
            }
            return info;
        }
        private static NewsInfo AllData(DataRow dr)
        {
            NewsInfo info = new NewsInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.NewsTypeID = DataConvert.ToInt(dr["NewsTypeID"]);
                info.Title = DataConvert.ToString(dr["Title"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.ClickCount = DataConvert.ToInt(dr["ClickCount"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateDateText = info.CreateDate.ToString("yyyy-MM-dd");
            }
            return info;
        }
        private static List<NewsInfo> ListSimpleData(DataTable dt)
        {
            List<NewsInfo> list = new List<NewsInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<NewsInfo> ListAllData(DataTable dt)
        {
            List<NewsInfo> list = new List<NewsInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

